package com.yupi.springbootinit.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

public class SingleProducer {
    private final static String SINGLE_QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        // 创建链接工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 设置 rabbitmq 对应的信息
/*        factory.setHost("xx.xx.xx.xx");
        factory.setUsername("xxx");
        factory.setPassword("xxx");*/


        // 建立链接，创建频道
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            // 创建消息队列
/* ● queueName：消息队列名称（注意，同名称的消息队列，只能用同样的参数创建一次）●  durabale：消息队列重启后，消息是否丢失，持久化处理 ●  exclusive：是否只允许当前这个创建消息队列的连接操作消息队列●  autoDelete：没有人用队列后，是否要删除队列*/
            channel.queueDeclare(SINGLE_QUEUE_NAME, false, false, false, null);
            String message = "Hello World!";
            channel.basicPublish("", SINGLE_QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }

    }
}